//
//  ZXRegisterTwoController.m
//  ZhuoxinAgriculture
//
//  Created by KKING on 2016/11/15.
//  Copyright © 2016年 KKING. All rights reserved.
//

#import "ZXRegisterTwoController.h"
#import "ZXLoginViewController.h"
#import "ZXZhuzhiChooseModel.h"

#import "CitiesView.h"
#import <QuartzCore/QuartzCore.h>
#import "SecondViewController.h"

@interface ZXRegisterTwoController ()<UIPickerViewDataSource,UIPickerViewDelegate>
{    
    NSString *_provinceId;
    NSString *_cityId;
    NSString *_areaId;
    
    NSString *_provinceName;
    NSString *_cityName;
    NSString *_areaName;
}


/**
 姓名 身份证
 */
@property (weak, nonatomic) IBOutlet UITextField *nameTf;
@property (weak, nonatomic) IBOutlet UITextField *sfzTf;


/**
 省市区 合作社
 */
@property (weak, nonatomic) IBOutlet UITextField *ssqTf;

@property (weak, nonatomic) IBOutlet UITextField *hzsTf;


@property (weak, nonatomic) IBOutlet UIButton *submitBtn;

//@property (weak, nonatomic) IBOutlet UITextField *nameTf;
//@property (weak, nonatomic) IBOutlet UITextField *nameTf;




/**
 隶属合作社 还是隶属农场?
 */
@property (weak, nonatomic) IBOutlet UILabel *zzChooseTitle;

/**
 选择合作社View
 */
@property (weak, nonatomic) IBOutlet UIView *chooseView;


/**
 选择键盘数据源数组
 */
@property (strong, nonatomic) NSMutableArray *ChoosezzArr;
//选择键盘
@property (strong, nonatomic) UIPickerView *hzsPickerView;

//组织id
@property (copy, nonatomic) NSString *companyNo;

@end

@implementation ZXRegisterTwoController


- (IBAction)choose:(id)sender {
    //    [self presentViewController:[XZViewController new] animated:YES completion:nil];
    //    [self.navigationController pushViewController:[XZViewController new] animated:YES];
    
    SecondViewController *address = [[SecondViewController alloc]init];
    WeakSelf(WSelf);
    address.blockAddress = ^(NSMutableDictionary *newAddress){
        
        WSelf.ssqTf.text = [NSString stringWithFormat:@"%@ %@ %@",newAddress[@"province"],newAddress[@"cityName"],newAddress[@"regionName"]];
        
        _provinceId = newAddress[@"provinceCode"];
        _cityId = newAddress[@"cityCode"];
        _areaId = newAddress[@"regionCode"];
        _provinceName = newAddress[@"province"];
        _cityName = newAddress[@"cityName"];
        _areaName = newAddress[@"regionName"];

        
        
        WSelf.hzsTf.text = @"";
        
        [[ZXHTTPManager sharedManager]vieworgbillWithProvCode:_provinceId cityCode:_cityId regiCode:_areaId custKind:self.roleType currentPage:@"1" completed:^(NSDictionary *jsonDict, NSError *error) {
            
            WSelf.ChoosezzArr = [NSMutableArray array];
            NSLog(@"%@ -- error%@",jsonDict,error);
            if ([jsonDict[@"status"] isEqualToString:@"0"]) {
                WSelf.ChoosezzArr = [ZXZhuzhiChooseModel mj_objectArrayWithKeyValuesArray:jsonDict[@"result"]];
                dispatch_async(dispatch_get_main_queue(), ^{
                    WSelf.hzsTf.inputView = self.hzsPickerView;
                    [WSelf.hzsTf reloadInputViews];
                });
            }
            
        }];
        
    };
    [self.navigationController pushViewController:address animated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"注册";
    
    self.ssqTf.text = @"黑龙江省 绥化市 北林区";
    _provinceId = @"230000";
    _cityId = @"231200";
    _areaId = @"231202";
    
    _provinceName = @"黑龙江省";
    _cityName = @"绥化市";
    _areaName = @"北林区";

    
    self.submitBtn.userInteractionEnabled = NO;
    self.submitBtn.alpha = 0.5;
    
    
    //监听按钮
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(change:) name:UITextFieldTextDidEndEditingNotification object:nil];
    
    
    
    if ([self.roleType isEqualToString:@"3"]) {
        self.chooseView.hidden = YES;
        return;
    }
    
    
    if ([self.roleType isEqualToString:@"2"]) {
        self.zzChooseTitle.text = @"隶属农场";
    }
    
    WeakSelf(Wself);
    //获取选择键盘内容
    [[ZXHTTPManager sharedManager]vieworgbillWithProvCode:@"230000" cityCode:@"231200" regiCode:@"231202" custKind:self.roleType currentPage:@"1" completed:^(NSDictionary *jsonDict, NSError *error) {
        
        Wself.ChoosezzArr = [NSMutableArray array];
        NSLog(@"%@ -- error%@",jsonDict,error);
        if ([jsonDict[@"status"] isEqualToString:@"0"]) {
            Wself.ChoosezzArr = [ZXZhuzhiChooseModel mj_objectArrayWithKeyValuesArray:jsonDict[@"result"]];
        }
        
    }];
    
    
    self.hzsTf.inputView = self.hzsPickerView;
    [self.hzsTf reloadInputViews];
}


-(void)change:(NSNotification *)notification
{
    //判断下一步按钮是否可以点击
    if (self.sfzTf.text.length>0&&self.nameTf.text.length>0) {
        self.submitBtn.userInteractionEnabled = YES;
        self.submitBtn.alpha = 1;
    }else{
        self.submitBtn.alpha = 0.5;
        self.submitBtn.userInteractionEnabled = NO;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)submitBtnClick:(id)sender {
    
    if (self.sfzTf.text.length <= 0) {
        HUDNormal(@"身份证号码不能为空");
        return;
    }
    else if (self.sfzTf.text.length != 18) {
        HUDNormal(@"身份证号码格式输入错误");
        return;
    }
    else if (self.nameTf.text.length <= 0){
        HUDNormal(@"姓名不能为空");
        return;
    }
    
    
    /*
     "password":"999999",
     "phone":"1822222222",
     "roleType":"1",
     
     "organId":"HZS1611220829",
     "registerTime":"201621123200000"
     */
    NSMutableDictionary * parameters = [NSMutableDictionary dictionary];
    parameters[@"userName"] = self.nameTf.text;
    parameters[@"certNo"] = self.sfzTf.text;
    parameters[@"photo"] = self.headIconImg;
    
    parameters[@"phone"] = self.phone;
    parameters[@"password"] = self.password;
    parameters[@"roleType"] = self.roleType;
    
    parameters[@"province"] = [NSString stringWithFormat:@"%@|%@",_provinceId,_provinceName];
    parameters[@"city"] = [NSString stringWithFormat:@"%@|%@",_cityId,_cityName];
    parameters[@"county"] = [NSString stringWithFormat:@"%@|%@",_areaId,_areaName];
    
    if ([self.roleType isEqualToString:@"3"]) {
        parameters[@"organId"] = @"HZS1611290917";
    }else{
        parameters[@"organId"] = self.companyNo;
    }
    parameters[@"registerTime"] = [NSDate new].toTimeStamp;
    
    [[ZXHTTPManager sharedManager] registerParam:parameters completed:^(NSDictionary *jsonDict, NSError *error) {
        
        if (error) {
            NSLog(@"error = %@",error);
            HUDNormal(@"请检查您的网络");
            
            return ;
        }
        HUDNormal(jsonDict[@"message"]);
        [NSThread sleepForTimeInterval:2.0];
        NSLog(@"jsonDict = %@",jsonDict);
        [self.navigationController popToRootViewControllerAnimated:NO];
    }];
    
}

///合作社选择
/**
 懒加载pickerView
 
 @return pickerView
 */
- (UIPickerView *)hzsPickerView{
    
    if (!_hzsPickerView) {
        _hzsPickerView = [[UIPickerView alloc]init];
        _hzsPickerView.delegate = self;
        _hzsPickerView.dataSource = self;
    }
    return _hzsPickerView;
}


//数据源方法
//返回列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

//返回行
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.ChoosezzArr.count + 1;
}

//代理方法
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    if (row == 0) {
        return @"请选择";
    }else{
        ZXZhuzhiChooseModel *model = self.ChoosezzArr[row - 1];
        return model.fullName;
    }
}

//代理方法
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    if (row == 0 ) {
        self.hzsTf.text = @"";
    }else{
        ZXZhuzhiChooseModel *model = self.ChoosezzArr[row - 1];
        self.hzsTf.text = model.fullName;
        self.companyNo = model.companyNo;
    }
}




@end
